package ejb.updater;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.quartz.Job;
import org.quartz.JobExecutionContext;

public abstract class SchedWeatherUpdater implements Job {

        private ConnectionFactory cf;
        private Queue queue;
        
        protected AltCity[] cities;
        protected AltCityForecast[][] forecasts;
        
        
        public static int FORECASTED_DAYS = 4;
        public static int CITIES = 7;
    /**
     * Default constructor. 
     */
    public SchedWeatherUpdater() {
                cities = new AltCity[CITIES];
                        
                for(int i=0; i<CITIES; i++) {
                        cities[i] = new AltCity();
                        cities[i].setId(i+1);
                }
                
                cities[0].setName("Warszawa");
                cities[1].setName("Poznan");
                cities[2].setName("Krakow");
                cities[3].setName("Wroclaw");
                cities[4].setName("Szczecin");
                cities[5].setName("Bialystok");
                cities[6].setName("Gdansk");
                
                forecasts = new AltCityForecast[FORECASTED_DAYS][];
                for(int i=0; i<FORECASTED_DAYS; i++) {
                        forecasts[i] = new AltCityForecast[CITIES];
                        for(int j=0; j<CITIES; j++) {
                                forecasts[i][j] = new AltCityForecast(cities[j], i);
                                forecasts[i][j].setPlusday(i+1);
                        }
                }
                
                InitialContext ic;
                try {
                        ic = new InitialContext();
                        cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
                        queue = (Queue)ic.lookup("/queue/KolejkaKomunikatow");
                } catch (NamingException e) {
                        System.err.println("Naming exception while initializing Weather Updater.");
                }
    }

        /**
     * @see Job#execute(JobExecutionContext)
     */
    public abstract void execute(JobExecutionContext arg0);

    
        protected void sendMessage(boolean update, AltCity city, boolean forecast, int plusday) {
                try {
                        Connection connection = cf.createConnection();
                        Session session = connection.createSession(false,  Session.AUTO_ACKNOWLEDGE);
                        MessageProducer producer = session.createProducer(queue);
                        ObjectMessage msg = session.createObjectMessage();
                        msg.setBooleanProperty("update", update);
                        msg.setBooleanProperty("forecast", forecast);
                        
                        //wiadomoæ z prognoz¹ - plusday = za ile dni
                        if(forecast)
                                msg.setIntProperty("plusday", plusday);
                        
                        //wiadomoæ nieodwiezajaca mapy
                        if(!update)
                                msg.setObject(city);
                                
                        producer.send(msg);
                        session.close();
                        connection.close();
                } catch(JMSException e) {
                        System.err.println("Message sending error while refreshing weather");
                }
        }

        protected static String makeUrl(String cityName) {
                return "http://api.wunderground.com/api/abe5803c3c26123c/geolookup/conditions/forecast/q/PL/"+cityName+".xml";
        }

}